﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls.Charting;
using FIFP.Common;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using Telerik.Windows.Controls;
using System.Windows.Messaging;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using FIFP.Report.Model;
using System.Linq;
using System.Xml.Serialization;
using System.ComponentModel;
using FIFP.Report.Report;

namespace FIFP.Report.ViewModel
{
    public class ReportViewModel : ViewModelCore, INotifyPropertyChanged
    {
        #region Variables
        private RadChart divisionLossChart;
        private RadChart divisionMonthLossChart;
        private DateTime startDate = new DateTime(DateTime.Now.Year, 1, 1);
        private DateTime endDate = new DateTime(DateTime.Now.Year, 12, 31);

        private LocalMessageReceiver localMessageECReceiver = new LocalMessageReceiver("ECFromSenderToReceiver",
                                                                ReceiverNameScope.Global,
                                                                LocalMessageReceiver.AnyDomain);

        private LocalMessageReceiver localMessageMCReceiver = new LocalMessageReceiver("MCFromSenderToReceiver",
                                                                ReceiverNameScope.Global,
                                                                LocalMessageReceiver.AnyDomain);


        #endregion

        #region Properties
        ObservableCollection<List<DataPoint>> _DivisionLossList = new ObservableCollection<List<DataPoint>>();
        public ObservableCollection<List<DataPoint>> DivisionLossList
        {
            get { return _DivisionLossList; }
            set { SetPropertyValue(() => DivisionLossList, value, val => _DivisionLossList = val); }
        }

        List<DataPoint> _EffectiveCapacityList = new List<DataPoint>();
        public List<DataPoint> EffectiveCapacityList
        {
            get { return _EffectiveCapacityList; }
            set { SetPropertyValue(() => EffectiveCapacityList, value, val => _EffectiveCapacityList = val); }
        }

        List<DataPoint> _AvailableOilCapacityList = new List<DataPoint>();
        public List<DataPoint> AvailableOilCapacityList
        {
            get { return _AvailableOilCapacityList; }
            set { SetPropertyValue(() => AvailableOilCapacityList, value, val => _AvailableOilCapacityList = val); }
        }


        List<DataPoint> _CapacityList = new List<DataPoint>();
        public List<DataPoint> CapacityList
        {
            get { return _CapacityList; }
            set { SetPropertyValue(() => CapacityList, value, val => _CapacityList = val); }
        }

        List<DataPoint> _ReservoirCapacityList = new List<DataPoint>();
        public List<DataPoint> ReservoirCapacityList
        {
            get { return _ReservoirCapacityList; }
            set { SetPropertyValue(() => ReservoirCapacityList, value, val => _ReservoirCapacityList = val); }
        }

        List<DataPoint> _MaxCapacityList = new List<DataPoint>();
        public List<DataPoint> MaxCapacityList
        {
            get { return _MaxCapacityList; }
            set { SetPropertyValue(() => MaxCapacityList, value, val => _MaxCapacityList = val); }
        }

        ObservableCollection<List<DataPoint>> _ReservoirCapacityCollection = new ObservableCollection<List<DataPoint>>();
        public ObservableCollection<List<DataPoint>> ReservoirCapacityCollection
        {
            get { return _ReservoirCapacityCollection; }
            set { SetPropertyValue(() => ReservoirCapacityCollection, value, val => _ReservoirCapacityCollection = val); }
        }


        ObservableCollection<List<DataPoint>> _MonthlyLossList = new ObservableCollection<List<DataPoint>>();
        public ObservableCollection<List<DataPoint>> MonthlyLossList
        {
            get { return _MonthlyLossList; }
            set { SetPropertyValue(() => MonthlyLossList, value, val => _MonthlyLossList = val); }
        }

        private string scenarioName = string.Empty;
        public string ScenarioName
        {
            get { return scenarioName; }
            set { SetPropertyValue(() => ScenarioName, value, val => scenarioName = val); }
        }


        private string capacityHeaderName = string.Empty;
        public string CapacityHeaderName
        {
            get { return capacityHeaderName; }
            set { SetPropertyValue(() => CapacityHeaderName, value, val => capacityHeaderName = val); }
        }

        private string ecCapacityHeaderName = string.Empty;
        public string EcCapacityHeaderName
        {
            get { return ecCapacityHeaderName; }
            set { SetPropertyValue(() => EcCapacityHeaderName, value, val => ecCapacityHeaderName = val); }
        }

        private string reservoirHeaderName = string.Empty;
        public string ReservoirHeaderName
        {
            get { return reservoirHeaderName; }
            set { SetPropertyValue(() => ReservoirHeaderName, value, val => reservoirHeaderName = val); }
        }

        private string lossHeaderName = string.Empty;
        public string LossHeaderName
        {
            get { return lossHeaderName; }
            set { SetPropertyValue(() => LossHeaderName, value, val => lossHeaderName = val); }
        }

        private string reportHeaderName = string.Empty;
        public string ReportHeaderName
        {
            get { return reportHeaderName; }
            set { SetPropertyValue(() => ReportHeaderName, value, val => reportHeaderName = val); }
        }


        Dictionary<string, List<DataPoint>> _MonthlyLoss = new Dictionary<string, List<DataPoint>>();
        public Dictionary<string, List<DataPoint>> MonthlyLoss
        {
            get { return _MonthlyLoss; }
            set { SetPropertyValue(() => MonthlyLoss, value, val => _MonthlyLoss = val); }
        }

        private double _EffectiveCapacityMaxValue = 400;
        public double EffectiveCapacityMaxValue
        {
            get { return _EffectiveCapacityMaxValue; }
            set { SetPropertyValue(() => EffectiveCapacityMaxValue, value, val => _EffectiveCapacityMaxValue = val); }
        }

        private double _ReservoirMaxValue = 400;
        public double ReservoirMaxValue
        {
            get { return _ReservoirMaxValue; }
            set { SetPropertyValue(() => ReservoirMaxValue, value, val => _ReservoirMaxValue = val); }
        }

        private double _FieldEfficiencyMaxValue = 400;
        public double FieldEfficiencyMaxValue
        {
            get { return _FieldEfficiencyMaxValue; }
            set { SetPropertyValue(() => FieldEfficiencyMaxValue, value, val => _FieldEfficiencyMaxValue = val); }
        }

        private double _DivisionMaxValue = 400;
        public double DivisionMaxValue
        {
            get { return _DivisionMaxValue; }
            set { SetPropertyValue(() => DivisionMaxValue, value, val => _DivisionMaxValue = val); }
        }

        private double _DivisionMonthY1MaxValue = 50;
        public double DivisionMonthY1MaxValue
        {
            get { return _DivisionMonthY1MaxValue; }
            set { SetPropertyValue(() => DivisionMonthY1MaxValue, value, val => _DivisionMonthY1MaxValue = val); }
        }

        private double _DivisionMonthMaxValue = 400;
        public double DivisionMonthMaxValue
        {
            get { return _DivisionMonthMaxValue; }
            set { SetPropertyValue(() => DivisionMonthMaxValue, value, val => _DivisionMonthMaxValue = val); }
        }

        private int selectedYear;
        public int SelectedYear
        {
            get { return selectedYear; }           
            set { SetPropertyValue(() => SelectedYear, value, val => selectedYear = val); }
        }
        private int selectedMCYear;
        public int SelectedMCYear
        {
            get { return selectedMCYear; }         
            set { SetPropertyValue(() => SelectedMCYear, value, val => selectedMCYear = val); }
        }

        private Visibility dropDownVisibility = Visibility.Collapsed;
        public Visibility DropDownVisibility
        {
            get { return dropDownVisibility; }
            set { SetPropertyValue(() => DropDownVisibility, value, val => dropDownVisibility = val); }
        }

        private List<int> yearList = new List<int>();
        public List<int> YearList
        {
            get { return yearList; }
            set { SetPropertyValue(() => YearList, value, val => yearList = val); }
        }

        List<EffectiveCapacityData> _RCList = new List<EffectiveCapacityData>();
        public List<EffectiveCapacityData> RCList
        {
            get { return _RCList; }
            set { SetPropertyValue(() => RCList, value, val => _RCList = val); }
        }
        List<EffectiveCapacityData> _ECList = new List<EffectiveCapacityData>();
        public List<EffectiveCapacityData> ECList
        {
            get { return _ECList; }
            set { SetPropertyValue(() => ECList, value, val => _ECList = val); }
        }

        List<EffectiveCapacityData> _OCList = new List<EffectiveCapacityData>();
        public List<EffectiveCapacityData> OCList
        {
            get { return _OCList; }
            set { SetPropertyValue(() => OCList, value, val => _OCList = val); }
        }

        List<EffectiveCapacityData> _MCList = new List<EffectiveCapacityData>();
        public List<EffectiveCapacityData> MCList
        {
            get { return _MCList; }
            set { SetPropertyValue(() => MCList, value, val => _MCList = val); }
        }

        List<LossByDivisionAndData> _DLList = new List<LossByDivisionAndData>();
        public List<LossByDivisionAndData> DLList
        {
            get { return _DLList; }
            set { SetPropertyValue(() => DLList, value, val => _DLList = val); }
        }

        List<LossByDivisionAndData> _MLList = new List<LossByDivisionAndData>();
        public List<LossByDivisionAndData> MLList
        {
            get { return _MLList; }
            set { SetPropertyValue(() => MLList, value, val => _MLList = val); }
        }

        #endregion

        #region Commands
        private ICommand _RadChartMouseLeftButtonCommand;
        public ICommand RadChartMouseLeftButtonCommand
        {
            get
            {
                return _RadChartMouseLeftButtonCommand ??
                        (_RadChartMouseLeftButtonCommand = new RelayCommand<RadChart>(ZoomInChartWithLabels));
            }
        }

        private ICommand _RadChartLoadedCommand;
        public ICommand RadChartLoadedCommand
        {
            get
            {
                return _RadChartLoadedCommand ??
                        (_RadChartLoadedCommand = new RelayCommand<RadChart>(LoadRadChart));
            }
        }

        private ICommand _DivisionMonthLossRadChartLoadedCommand;
        public ICommand DivisionMonthLossRadChartLoadedCommand
        {
            get
            {
                return _DivisionMonthLossRadChartLoadedCommand ??
                        (_DivisionMonthLossRadChartLoadedCommand = new RelayCommand<RadChart>(LoadDivisionMonthLossRadChart));
            }
        }        

        private ICommand _YearSelectionCommand;
        public ICommand YearSelectionCommand
        {
            get
            {
                return _YearSelectionCommand ??
                        (_YearSelectionCommand = new RelayCommand<ComboBox>(LoadYearlyData));
            }
        }

        #endregion

        #region Constructor
        public ReportViewModel()
        {
            //if (!DesignerProperties.IsInDesignTool)
            {
                if (App.QueryStringId.ToLower() == "ec")
                {
                    localMessageECReceiver = new LocalMessageReceiver(App.EcSenderName,
                                                                    ReceiverNameScope.Global,
                                                                    LocalMessageReceiver.AnyDomain);

                    localMessageECReceiver.MessageReceived += LocalMessageECReceiverMessageReceived;
                    localMessageECReceiver.Listen();
                }
                else
                {
                    localMessageMCReceiver = new LocalMessageReceiver(App.McSenderName,
                                                                    ReceiverNameScope.Global,
                                                                    LocalMessageReceiver.AnyDomain);
                    localMessageMCReceiver.MessageReceived += LocalMessageMCReceiverMessageReceived;
                    localMessageMCReceiver.Listen();
                }
            }
        }

        void LocalMessageMCReceiverMessageReceived(object sender, MessageReceivedEventArgs e)
        {
            if (e.Message != null)
            {
                try
                {
                    Reset();
                    DropDownVisibility = Visibility.Visible;
                    var ser = new XmlSerializer(typeof(ReportData));
                    ReportData reportData = (ReportData)ser.Deserialize(new StringReader(e.Message));
                    if (reportData != null)
                    {
                        ScenarioName = reportData.ScenarioName;
                        ReportHeaderName = "Umm Shaif 100% Efficient Capacity Reports";
                        MCList = reportData.MCList;
                        RCList = reportData.RCList;
                        YearList = (from item in reportData.MCList
                                    select item.Date.Year).Distinct().ToList<int>();
                        SelectedMCYear = YearList[0];
                        UpdateChartByYear("mc", SelectedMCYear);
                    }
                }
                catch (Exception ex)
                { }
            }
        }

        void LocalMessageECReceiverMessageReceived(object sender, MessageReceivedEventArgs e)
        {
            if (e.Message != null)
            {
                try
                {
                    Reset();
                    DropDownVisibility = Visibility.Collapsed;
                    var ser = new XmlSerializer(typeof(ReportData));
                    ReportData reportData = (ReportData)ser.Deserialize(new StringReader(e.Message));
                    if (reportData != null)
                    {
                        ReportHeaderName = "Umm Shaif Field Effective Capacity Reports";
                        ScenarioName = reportData.ScenarioName;

                        ECList = reportData.ECList;
                        RCList = reportData.RCList;
                        MCList = reportData.MCList;
                        MLList = reportData.MonthlyLossList;
                        OCList = reportData.OilList;
                        //DLList = reportData.DivisionLossList;




                        YearList = (from item in reportData.ECList
                                    select item.Date.Year).Distinct().ToList<int>();
                        SelectedYear = YearList[0];
                        UpdateChartByYear("ec", SelectedYear);
                    }
                }
                catch (Exception ex)
                { }
            }
        }

        #endregion

        private void UpdateChartByYear(string selectedChartType, int SelectedYear)
        {
            if (selectedChartType == "mc")
            {
                LoadMaxCapacityData((from item in MCList
                                     where item.Date.Year == SelectedYear
                                     select item).ToList<EffectiveCapacityData>());
                LoadReservoirCapacityData((from item in RCList
                                           where item.Date.Year == SelectedYear
                                           select item).ToList<EffectiveCapacityData>());
            }
            else if (selectedChartType == "ec")
            {
                LoadEffectiveCapacityData((from item in ECList
                                           where item.Date.Year == SelectedYear
                                           select item).ToList<EffectiveCapacityData>());
                LoadReservoirCapacityData((from item in RCList
                                           where item.Date.Year == SelectedYear
                                           select item).ToList<EffectiveCapacityData>());
                LoadMaxCapacityData((from item in MCList
                                     where item.Date.Year == SelectedYear
                                     select item).ToList<EffectiveCapacityData>());
                LoadAvailableOilCapacityData((from item in OCList
                                              where item.Date.Year == SelectedYear
                                              select item).ToList<EffectiveCapacityData>());

                //For initialization purpose
                MonthlyLossList.Add(ReservoirCapacityList);
                MonthlyLossList.Add(EffectiveCapacityList);
                MonthlyLossList.Add(MaxCapacityList);
                MonthlyLossList.Add(AvailableOilCapacityList);

                //Monthly Loss
                LoadMonthlyLossData((from item in MLList
                                     where item.Date.Year == SelectedYear
                                     select item).ToList<LossByDivisionAndData>());

                //Division Loss
                List<LossByDivisionAndData> selectedYeardivisionLossList = new List<LossByDivisionAndData>();
                List<EffectiveCapacityData> selectedYearRCData = (from item in RCList
                                                                  where item.Date.Year == SelectedYear
                                                                  select item).ToList<EffectiveCapacityData>();
                LossByDivisionAndData selectedYearRCLossData = new LossByDivisionAndData() { Division = "Res.C", Loss = Math.Round(selectedYearRCData.Average(s => s.EffectiveCapacity), 2) };
                selectedYeardivisionLossList.Add(selectedYearRCLossData);


                selectedYeardivisionLossList.AddRange((from item in MLList
                                                       where item.Date.Year == SelectedYear
                                                       group item by new { item.Division } into g
                                                       select new LossByDivisionAndData()
                                                       {
                                                           Division = g.Key.Division,
                                                           Loss = Math.Round(Math.Round(g.Sum(s => s.Loss), 2) / 12, 2),
                                                           Date = new DateTime(SelectedYear, 1, 1)
                                                       }).ToList<LossByDivisionAndData>());
                List<EffectiveCapacityData> selectedYearECData = (from item in ECList
                                                                  where item.Date.Year == SelectedYear
                                                                  select item).ToList<EffectiveCapacityData>();
                LossByDivisionAndData selectedYearECLossData = new LossByDivisionAndData() { Division = "Eff.C", Loss = Math.Round(selectedYearECData.Average(s => s.EffectiveCapacity), 2) };
                selectedYeardivisionLossList.Add(selectedYearECLossData);
                LoadDivisionLossData(selectedYeardivisionLossList);
            }
        }

        #region Load yearly Data
        private void LoadYearlyData(ComboBox cmbYear)
        {
            if (cmbYear != null && cmbYear.Tag != null)
            {
                string selectedChartType = cmbYear.Tag.ToString();
                if (!string.IsNullOrEmpty(selectedChartType))
                {
                    Reset();
                    if (selectedChartType == "ec")
                        UpdateChartByYear(selectedChartType, SelectedYear);
                    else if (selectedChartType == "mc")
                        UpdateChartByYear(selectedChartType, SelectedMCYear);
                }
            }
        }

        #endregion

        private void Reset()
        {
            ReservoirCapacityCollection = new ObservableCollection<List<DataPoint>>();
            ReservoirCapacityList = new List<DataPoint>();
            EffectiveCapacityList = new List<DataPoint>();
            MaxCapacityList = new List<DataPoint>();
            MonthlyLoss = new Dictionary<string, List<DataPoint>>();
            MonthlyLossList = new ObservableCollection<List<DataPoint>>();
            DivisionLossList = new ObservableCollection<List<DataPoint>>();
            CapacityList = new List<DataPoint>();
            AvailableOilCapacityList = new List<DataPoint>();
        }

        #region Methods
        private void ZoomInChartWithLabels(RadChart radChart)
        {
            DivisionLossPopup popup = new DivisionLossPopup();
            popup.Title = "Losses(monthly)";
            BindDivisionMonthlyLossData(popup.popupRadChart, true);
            popup.LayoutRoot.DataContext = this;
            popup.Show();
        }

        private void LoadDivisionLossData(List<LossByDivisionAndData> divisionLoss)
        {
            LossHeaderName = "Total Loss - " + divisionLoss.Where(p => p.Division != "Res.C" && p.Division != "Eff.C").Sum(s => s.Loss) + " MBD";

            DivisionLossList = new ObservableCollection<List<DataPoint>>();
            DivisionLossList.Add(new List<DataPoint>() { new DataPoint() { Low = 0, High = divisionLoss[0].Loss, Label = divisionLoss[0].Loss.ToString(), XCategory = divisionLoss[0].Division } });

            double low = divisionLoss[0].Loss - divisionLoss[1].Loss;
            double high = divisionLoss[0].Loss;

            DivisionLossList.Add(new List<DataPoint>() { new DataPoint() { Low = low, High = high, Label = divisionLoss[1].Loss.ToString(), XCategory = divisionLoss[1].Division } });

            if (divisionLoss.Count > 2)
            {
                int i = 2;
                while (i < divisionLoss.Count - 1)
                {
                    DivisionLossList.Add(new List<DataPoint>() { new DataPoint() { Low = low - divisionLoss[i].Loss, High = low, Label = (divisionLoss[i].Loss).ToString(), XCategory = divisionLoss[i].Division } });
                    low = low - divisionLoss[i].Loss;
                    i++;
                }

                DivisionLossList.Add(new List<DataPoint>() { new DataPoint() { Low = 0, High = divisionLoss[divisionLoss.Count - 1].Loss, Label = divisionLoss[divisionLoss.Count - 1].Loss.ToString(), XCategory = divisionLoss[divisionLoss.Count - 1].Division } });
            }
            if (DivisionLossList != null && DivisionLossList.Count > 0)
            {
                if (divisionLossChart != null)
                {
                    if (divisionLossChart.SeriesMappings.Count > 0)
                        divisionLossChart.SeriesMappings.Clear();
                    BindDivisionLoassData();
                }
            }
        }

        private void BindDivisionLoassData()
        {
            int nIndex = 0;
            DivisionMaxValue = DivisionMonthY1MaxValue = DivisionLossList.Max(s => s.Max(p => p.YValue));
            if (DivisionMaxValue < 400)
                DivisionMaxValue = 400;

            if (DivisionMonthY1MaxValue < 50)
                DivisionMonthY1MaxValue = 50;

            foreach (List<DataPoint> point in DivisionLossList)
            {
                SeriesMapping mapping = new SeriesMapping();

                mapping.CollectionIndex = nIndex;

                RangeBarSeriesDefinition seriesDefinition = new RangeBarSeriesDefinition();
                seriesDefinition.SeriesItemLabelStyle = Application.Current.Resources["SeriesItemLabelStyle"] as Style;
                GenerateSeriesColor(point[0].XCategory, seriesDefinition);
                mapping.SeriesDefinition = seriesDefinition;
                point[0].XCategory = (point[0].XCategory == "Brown Fields") ? "Bro.F" : (point[0].XCategory == "Maintenance") ? "Maint" : (point[0].XCategory == "Drilling") ? "Dril" : (point[0].XCategory == "Integrity") ? "Int" : (point[0].XCategory == "EST/TSD") ? "TSD" : point[0].XCategory;
                mapping.SeriesDefinition.ShowItemLabels = true;
                //mapping.SeriesDefinition.ShowItemToolTips = true;
                mapping.ItemMappings.Add(new ItemMapping("Low", DataPointMember.Low));
                mapping.ItemMappings.Add(new ItemMapping("High", DataPointMember.High));
                mapping.ItemMappings.Add(new ItemMapping("Label", DataPointMember.Label));
                mapping.ItemMappings.Add(new ItemMapping("XCategory", DataPointMember.XCategory));
                divisionLossChart.SeriesMappings.Add(mapping);
                nIndex++;
            }
            divisionLossChart.ItemsSource = DivisionLossList;
        }

        private void GenerateSeriesColor(string division, SeriesDefinition seriesDefinition)
        {
            switch (division)
            {
                case "Brown Fields":
                    seriesDefinition.Appearance.Fill = new SolidColorBrush(Colors.Orange);
                    break;
                case "Maintenance":
                    seriesDefinition.Appearance.Fill = new SolidColorBrush(Colors.DarkGray);
                    break;
                case "Drilling":
                    seriesDefinition.Appearance.Fill = new SolidColorBrush(Colors.Blue);
                    break;
                case "OGCD":
                    seriesDefinition.Appearance.Fill = new SolidColorBrush(Colors.Cyan);
                    break;
                case "Integrity":
                    seriesDefinition.Appearance.Fill = new SolidColorBrush(Colors.Magenta);
                    break;
                case "PTPD":
                    seriesDefinition.Appearance.Fill = new SolidColorBrush(Colors.Purple);
                    break;
                case "FDD (US)":
                    seriesDefinition.Appearance.Fill = new SolidColorBrush(Colors.Red);
                    break;
                case "CPLD":
                    seriesDefinition.Appearance.Fill = new SolidColorBrush(Colors.Brown);
                    break;
                case "EST/TSD":
                    seriesDefinition.Appearance.Fill = new SolidColorBrush(Colors.Yellow);
                    break;
                case "Res.C":
                    seriesDefinition.Appearance.Fill = new SolidColorBrush(Colors.Green);
                    break;
                case "Eff.C":
                    seriesDefinition.Appearance.Fill = new SolidColorBrush(new Color() { A = 0xFF, R = 0x4E, G = 0xCF, B = 0x13 });
                    break;
            }
        }

        private void LoadRadChart(RadChart radChart)
        {
            divisionLossChart = radChart;
            if (DivisionLossList != null && DivisionLossList.Count > 0)
            {
                if (divisionLossChart != null)
                {
                    if (divisionLossChart.SeriesMappings.Count > 0)
                        divisionLossChart.SeriesMappings.Clear();
                    BindDivisionLoassData();
                }
            }
        }       

        private void LoadDivisionMonthLossRadChart(RadChart radChart)
        {
            divisionMonthLossChart = radChart;
            //if (MonthlyLoss != null && MonthlyLoss.Count > 0)
            {
                if (divisionMonthLossChart != null)
                {
                    if (divisionMonthLossChart.SeriesMappings.Count > 0)
                        divisionMonthLossChart.SeriesMappings.Clear();
                    BindDivisionMonthlyLossData(divisionMonthLossChart, false);
                }
            }
        }

        private void LoadAvailableOilCapacityData(List<EffectiveCapacityData> ocList)
        {
            List<DataPoint> oilDataList = new List<DataPoint>();
            foreach (EffectiveCapacityData data in ocList)
                oilDataList.Add(new DataPoint() { YValue = data.EffectiveCapacity, XCategory = data.Date.ToString("MMM") });

            AvailableOilCapacityList = oilDataList;
        }



        private void LoadEffectiveCapacityData(List<EffectiveCapacityData> ecList)
        {
            EcCapacityHeaderName = ScenarioName + " - Effective Capacity - " + Math.Round(ecList.Average(s => s.EffectiveCapacity), 2) + " MBD";
            List<DataPoint> ecDataList = new List<DataPoint>();
            foreach (EffectiveCapacityData data in ecList)
                ecDataList.Add(new DataPoint() { YValue = data.EffectiveCapacity, XCategory = data.Date.ToString("MMM") });

            double maxValue = ecDataList.Max(s => s.YValue);
            if (maxValue > 400)
                EffectiveCapacityMaxValue = maxValue;
            else
                EffectiveCapacityMaxValue = 400;

            EffectiveCapacityList = ecDataList;
        }

        private void LoadReservoirCapacityData(List<EffectiveCapacityData> rcList)
        {
            //MonthlyLossList.Add(ReservoirCapacityList);
            ReservoirHeaderName = "Reservoir Capacity - " + Math.Round(rcList.Average(s => s.EffectiveCapacity), 2) + " MBD";

            List<DataPoint> ecDataList = new List<DataPoint>();
            foreach (EffectiveCapacityData data in rcList)
                ecDataList.Add(new DataPoint() { YValue = data.EffectiveCapacity, XCategory = data.Date.ToString("MMM") });

            ReservoirCapacityList = ecDataList;

            double maxValue = ReservoirCapacityList.Max(s => s.YValue);
            if (maxValue > 400)
                ReservoirMaxValue = maxValue;
            else
                ReservoirMaxValue = 400;
        }

        private void LoadMaxCapacityData(List<EffectiveCapacityData> mcList)
        {
            CapacityHeaderName = "100% Efficient Capacity - " + Math.Round(mcList.Average(s => s.EffectiveCapacity), 2) + " MBD";
            List<DataPoint> ecDataList = new List<DataPoint>();
            foreach (EffectiveCapacityData data in mcList)
                ecDataList.Add(new DataPoint() { YValue = data.EffectiveCapacity, XCategory = data.Date.ToString("MMM") });
            MaxCapacityList = ecDataList;

            double maxValue = MaxCapacityList.Max(s => s.YValue);
            if (maxValue > FieldEfficiencyMaxValue)
                FieldEfficiencyMaxValue = maxValue;
            else
                FieldEfficiencyMaxValue = 400;
        }

        private void LoadMonthlyLossData(List<LossByDivisionAndData> monthlyLossList)
        {
            if (monthlyLossList != null && monthlyLossList.Count > 0)
            {
                var query = from item in monthlyLossList
                            group item by item.Division into g
                            select new { Division = g.Key, Values = g };

                foreach (var item1 in query)
                {
                    List<DataPoint> lstLoss = new List<DataPoint>();
                    foreach (LossByDivisionAndData loss in item1.Values)
                    {
                        lstLoss.Add(new DataPoint() { YValue = loss.Loss, XCategory = loss.Date.ToString("MMM") });
                    }
                    MonthlyLoss.Add(item1.Division, lstLoss);
                }
            }

            //if (MonthlyLoss != null && MonthlyLoss.Count > 0)
            {
                if (divisionMonthLossChart != null)
                {
                    if (divisionMonthLossChart.SeriesMappings.Count > 0)
                        divisionMonthLossChart.SeriesMappings.Clear();
                    BindDivisionMonthlyLossData(divisionMonthLossChart, false);
                }
            }
        }

        private void BindDivisionMonthlyLossData(RadChart divisionMonthLossChart, bool isShowLabel)
        {
            int nIndex = 0;
            ObservableCollection<List<DataPoint>> monthlyDivisionLossList = new ObservableCollection<List<DataPoint>>();
            if (ReservoirCapacityList != null && ReservoirCapacityList.Count > 0)
            {
                SeriesMapping mapping = new SeriesMapping();
                mapping.CollectionIndex = nIndex;
                mapping.LegendLabel = "Res.Cap";
                LineSeriesDefinition seriesDefinition = new LineSeriesDefinition();
                seriesDefinition.ShowItemLabels = isShowLabel ? true : false;
                seriesDefinition.Appearance.Stroke = new SolidColorBrush(Colors.Red);
                mapping.SeriesDefinition = seriesDefinition;
                mapping.SeriesDefinition.ShowItemToolTips = true;
                mapping.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue));
                mapping.ItemMappings.Add(new ItemMapping("XCategory", DataPointMember.XCategory));
                divisionMonthLossChart.SeriesMappings.Add(mapping);
                monthlyDivisionLossList.Add(ReservoirCapacityList);
                nIndex++;
            }

            if (AvailableOilCapacityList != null && AvailableOilCapacityList.Count > 0)
            {
                SeriesMapping mapping = new SeriesMapping();
                mapping.CollectionIndex = nIndex;
                mapping.LegendLabel = "Oil.Cap";
                LineSeriesDefinition seriesDefinition = new LineSeriesDefinition();
                seriesDefinition.ShowItemLabels = isShowLabel ? true : false;
                seriesDefinition.Appearance.Stroke = new SolidColorBrush(Colors.Green);
                mapping.SeriesDefinition = seriesDefinition;
                mapping.SeriesDefinition.ShowItemToolTips = true;
                mapping.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue));
                mapping.ItemMappings.Add(new ItemMapping("XCategory", DataPointMember.XCategory));
                divisionMonthLossChart.SeriesMappings.Add(mapping);
                monthlyDivisionLossList.Add(AvailableOilCapacityList);
                nIndex++;
            }

            foreach (string division in MonthlyLoss.Keys)
            {
                SeriesMapping mapping = new SeriesMapping();

                mapping.CollectionIndex = nIndex;
                StackedBarSeriesDefinition seriesDefinition = new StackedBarSeriesDefinition();
                seriesDefinition.SeriesItemLabelStyle = Application.Current.Resources["SeriesItemLabelStyle"] as Style;
                seriesDefinition.ShowItemLabels = isShowLabel ? true : false;
                GenerateSeriesColor(division, seriesDefinition);
                mapping.SeriesDefinition = seriesDefinition;
                mapping.LegendLabel = (division == "Brown Fields") ? "Bro.F" : (division == "Maintenance") ? "Maint" : (division == "Drilling") ? "Dril" : (division == "Integrity") ? "Int" : division;
                mapping.SeriesDefinition.ShowItemToolTips = true;
                mapping.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue));
                mapping.ItemMappings.Add(new ItemMapping("XCategory", DataPointMember.XCategory));
                divisionMonthLossChart.SeriesMappings.Add(mapping);
                monthlyDivisionLossList.Add(MonthlyLoss[division]);
                nIndex++;
            }

            if (EffectiveCapacityList != null && EffectiveCapacityList.Count > 0)
            {
                SeriesMapping mapping = new SeriesMapping();
                mapping.CollectionIndex = nIndex;
                mapping.LegendLabel = "Eff.Cap";
                LineSeriesDefinition seriesDefinition = new LineSeriesDefinition();
                seriesDefinition.Appearance.Stroke = new SolidColorBrush(new Color() { R = 0x00, G = 0xFF, B = 0x00, A = 0xFF }); 
                mapping.SeriesDefinition = seriesDefinition;
                seriesDefinition.ShowItemLabels = isShowLabel ? true : false;
                mapping.SeriesDefinition.ShowItemToolTips = true;
                mapping.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue));
                mapping.ItemMappings.Add(new ItemMapping("XCategory", DataPointMember.XCategory));
                divisionMonthLossChart.SeriesMappings.Add(mapping);
                monthlyDivisionLossList.Add(EffectiveCapacityList);
                nIndex++;
            }
            if (MaxCapacityList != null && MaxCapacityList.Count > 0)
            {

                SeriesMapping mapping = new SeriesMapping();
                mapping.CollectionIndex = nIndex;
                mapping.LegendLabel = "100% Effi.Cap";
                LineSeriesDefinition seriesDefinition = new LineSeriesDefinition();
                seriesDefinition.Appearance.Stroke = new SolidColorBrush(Colors.Yellow);
                mapping.SeriesDefinition = seriesDefinition;
                seriesDefinition.ShowItemLabels = isShowLabel ? true : false;
                mapping.SeriesDefinition.ShowItemToolTips = true;
                mapping.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue));
                mapping.ItemMappings.Add(new ItemMapping("XCategory", DataPointMember.XCategory));
                divisionMonthLossChart.SeriesMappings.Add(mapping);
                monthlyDivisionLossList.Add(MaxCapacityList);
                nIndex++;
            }

            double maxValue = monthlyDivisionLossList.Max(s => s.Max(p => p.YValue));
            if (maxValue > 400)
                DivisionMonthMaxValue = maxValue;
            else
                DivisionMonthMaxValue = 400;

            divisionMonthLossChart.ItemsSource = monthlyDivisionLossList;
        }
        #endregion
    }
}
